#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
# Program to create GParted live, based on Debian Live helper.

# load config files
. /live-hook-dir/ocs-live-hook.conf
# load functions
. /live-hook-dir/ocs-live-hook-functions 

# Overwrite the locale_to_keep in /live-hook-dir/ocs-live-hook.conf. We just keep C and en_US only. Later we will restore the gparted locales file.
locale_to_keep="C.UTF-8, en_US.UTF-8"

# Set the environment variable LIVE_CREATING_MODE so we know what we are doing
LIVE_CREATING_MODE="gparted"
export LIVE_CREATING_MODE

# Initial flag for updating initramfs
update_initramfs_flag="no"

# LIVE_CREATING_MODE will be used in download_grub_1_2_deb_for_later_use
download_grub_1_2_deb_for_later_use

#
clean_udev_persistent_net_rules

# create a dummy /etc/fstab in Live CD so that nis/yp won't complain
touch /etc/fstab

# Append /sbin and /usr/sbin in the system PATH 
echo "export PATH=\$PATH:/sbin:/usr/sbin" >> /etc/profile
echo "export PATH=\$PATH:/sbin:/usr/sbin" >> /etc/bash.bashrc

# Before localepurge, save gparted locales. We will put it back in the end (after any apt action)
echo "Backup gparted locales..."
( cd /usr/share/locale 
  find -iname "gparted*" -print | xargs tar -cf /gparted-locale.tar
)

#
install_debian_extra_modules

# Some packages to be removed
for i in $pkg_2_be_removed_for_gparted_live; do
  if LC_ALL=C dpkg -L $i &>/dev/null; then
    apt-get -y --purge remove $i
  fi
done

# clean some locales
set_localepurge
localepurge

# Turn off some services
for i in $service_2_be_off_for_gparted_live; do
  remove_service_in_system $i
done

# Use timedatectl to disable ntp
set_ntp_off

# Disable sleep and hibernate
disable_sleep_hibernate

# append the module so that it will be loaded, then gparted can grow filesystem
# Ref: http://gparted.sourceforge.net/features.php
append_mod_in_etc_modules

# set root passwd (maybe none)
set_root_passwd

# No more use this. Let live-initramfs deal with that.
# We have to create account user, otherwise there is some problem. See ocs-live-hook-functions for more details.
# preseed_autologin_account

# If lxterminal or mlterm is found with xfce installed, set default x-terminal-emulator as mlterm. Since xterm is not as good as mlterm in Traditional Chinese font.
if dpkg -L lxterminal &>/dev/null; then
  echo "Set lxterminal as default x-terminal-emulator."
  update-alternatives --set x-terminal-emulator /usr/bin/lxterminal
elif dpkg -L mlterm &>/dev/null; then
  echo "Set mlterm as default x-terminal-emulator."
  update-alternatives --set x-terminal-emulator /usr/bin/mlterm
  # change the fg/bg color
  perl -pi -e "s/^.*fg_color=.*/fg_color=white/g" /etc/mlterm/main
  perl -pi -e "s/^.*bg_color=.*/bg_color=black/g" /etc/mlterm/main
fi

# since ssh services is on, and autologin account is known for the whole world, we have to block it.
block_all_clients_by_tcpwrapper

# fluxbox menu, icons, exe files and others...
cp -af /live-hook-dir/gparted/fluxbox/menu /etc/X11/fluxbox/fluxbox.menu-user
cp -af /live-hook-dir/gparted/usr /

# put some desktop icons in the root account, when client boot, we will need them.
gparted_desktop_rep="/root/gparted-live/"
mkdir -p $gparted_desktop_rep
cp -af /live-hook-dir/gparted/ideskrc $gparted_desktop_rep/ideskrc
cp -af /live-hook-dir/gparted/idesktop $gparted_desktop_rep/idesktop
# //NOTE// We can not just put apps into ~$autologin_account/.fluxbox, otherwise some other files won't be put by startfluxbox when fluxbox is run first time. Therefore we put apps in home dir, then after fluxbox is started, put it into .fluxbox/
cp -af /live-hook-dir/gparted/fluxbox/apps $gparted_desktop_rep

# ALT+F5 invokes dangerous & irreversible program xkill,
# should not be on a kb shortcut
# https://bugzilla.gnome.org/show_bug.cgi?id=703400
perl -pi -e "s/^Mod1 F5 :Kill/# Mod1 F5 :Kill # commented by GParted live/g" /etc/X11/fluxbox/keys

# Put start scripts
mkdir -p /etc/gparted-live/
cp -ar /live-hook-dir/gparted/gparted-live.d /etc/gparted-live/

# Put main gparted live start service
# (1) Prepare the gparted live starting service
# (2) Prepare autologin for tty*
cp -af /live-hook-dir/systemd/start-gparted-live.service /usr/lib/systemd/system/; chown root:root /usr/lib/systemd/system/start-gparted-live.service
( cd /etc/systemd/system/getty.target.wants/; ln -fs /usr/lib/systemd/system/start-gparted-live.service .)
# Disable the booting status, otherwise the messages might be shown on the dialog menu (of keyboard/language for Clonezilla/GParted live) which is annoying.
perl -pi -e "s/^[#]ShowStatus=.*/ShowStatus=no/g" /etc/systemd/system.conf
for i in `seq 1 6`; do
  # tty1-6
  mkdir /etc/systemd/system/getty\@tty${i}.service.d/
  cp -af /live-hook-dir/systemd/tty-autologin-override.conf /etc/systemd/system/getty\@tty${i}.service.d/override.conf
  chown root:root /etc/systemd/system/getty\@tty${i}.service.d/override.conf
done
for i in `seq 0 3`; do
  # ttyS0-3
  mkdir /etc/systemd/system/serial-getty\@ttyS${i}.service.d/
  cp -af /live-hook-dir/systemd/serial-console-autologin.conf /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
  chown root:root /etc/systemd/system/serial-getty\@ttyS${i}.service.d/autologin.conf
done
# Put the script "start-gparted-live" to start /etc/gparted/gparted.d/
cp -af /live-hook-dir/start-gparted-live /etc/gparted-live/

# Put a link for vol_id so that GParted can use it to read linux-swap labels
if [ -e /usr/lib/udev/vol_id ]; then
  (cd /usr/sbin; ln -fs /usr/lib/udev/vol_id vol_id)
fi

# we need real /sbin/start-stop-daemon
remove_start_stop_daemon_diverts

# Since we remove_start_stop_daemon_diverts, therefore we have to set it back so the rest of chroot_dpkg of live-build v3.x won't remove the real /sbin/start-stop-daemon. The reason we remove then set is because we have to keep compatable with live-build v2.x. In the future when we switched to live-build v3.x completely, both "remove_start_stop_daemon_diverts" and "set_start_stop_daemon_diverts" for ocs-live-hook can be removed. //NOTE// They can not be removed in drbl-live-hook when live-build v3.x is used in the future.
set_start_stop_daemon_diverts

# Dirty hacking: rm files
echo "Starting dirty hacking to remove files..."
# Example:
# //NOTE// list them very carefully, otherwise maybe due to dependence, required package will be removed!
unnecessary_packages="xorg-docs-core xfonts-100dpi xfonts-75dpi xfonts-scalable"
if [ -n "$unnecessary_packages" ]; then
  echo "Force to remove some packages ($unnecessary_packages) if installed..."
  for i in $unnecessary_packages; do
   if dpkg -L $i &>/dev/null; then	   
     echo "Force to removing $i..."
     apt-get --yes --force-yes --purge remove $i
   fi
  done
fi

#
decide_if_use_xz_compression_for_initrd

# We might need more /lib/udev/*_id than that initramfs-tools copies. E.g. for Live CD, we need cdrom_id, which udev (>= version 146 won't copy).
copy_all_dev_id_prog_from_udev_lib

#
add_cpu_offline_online_in_initrd

# Append extra modules included in initramfs, since some NIC modules (e.g. atl2) will not be included in initramfs, those driver is required for PXE booting. 
# Ref: https://sourceforge.net/tracker/?func=detail&atid=671650&aid=2854969&group_id=115473
if [ -e "/etc/initramfs-tools/modules" ]; then
  echo "# The following modules were added by GParted" >> /etc/initramfs-tools/modules
  for i in $extra_module_list_in_initramfs; do
    echo "$i" >> /etc/initramfs-tools/modules
  done 
  update_initramfs_flag="yes"
fi


# Keep the parameters.txt, avoid it to be removed later.
# live-initramfs want to copy parameters.txt to live cd
[ -e /usr/share/doc/live-initramfs/parameters.txt ] && {
  cp -p /usr/share/doc/live-initramfs/parameters.txt /
}
for i in $share_dir_2_be_rm; do
  # Here we remove files only, but keep dirs so that later if user want to install packages, postrun won't exit because no /usr/share/man/man1, for example.
  # rm -rf /usr/share/$i/*
  find /usr/share/$i/ -type f -exec rm -f {} \;
done
# put it back
[ -e /parameters.txt ] && {
  mkdir -p /usr/share/doc/live-initramfs/
  mv -f /parameters.txt /usr/share/doc/live-initramfs/
}

#
dist="$(cat /etc/debian_version)"
if [ "$dist" = "4.0" ]; then
  # etch
  sed -i 's/libxrender1, gksu/libxrender1/' /var/lib/dpkg/status
else
  # lenny
  sed -i 's/Depends: gksu, libatk1.0-0/Depends: libatk1.0-0/' /var/lib/dpkg/status
fi
echo "gparted hold" | dpkg --set-selections
#apt-get remove --purge --yes --force-yes gksu libgksu2-0

#
if [ -n "$(LANG=C apt-get --help 2>&1 | grep autoremove)" ]; then
  apt-get -y autoremove
fi

# Just clean them
# Log
# Log, clean them, but keep the file name. i.e. make the size 0, otherwise if we clean them, some daemon will complain.
find /var/log/ -type f | xargs -I % bash -c "echo -n '' > %"
# CPP
find /usr/lib/gcc/ -name "cc1" -exec rm {} \;
find /usr/bin/ -name "cpp*" -exec rm {} \;
# Forget about removing zoneinfo.... This might break live-config.
#if [ -e /usr/share/zoneinfo/UTC ]; then
#  rm -f /etc/localtime # The original is a soft link file to UTC
#  cp -af /usr/share/zoneinfo/UTC /etc/localtime
#  rm -rf /usr/share/zoneinfo/*
#  # Restore UTC only to avoind live-initramfs giving error
#  cp -af /etc/localtime /usr/share/zoneinfo/UTC 
#fi
# Forget about removing gconv, since some program, e.g. mtools 4.10 need that.
# [ -d /usr/lib/gconv/ ] && rm -rf /usr/lib/gconv/*
[ -d /var/backups ] && rm -rf /var/backups/*
# gconf2* gconf2-common* gksu* gnome-keyring* libgconf2-4* libgksu2-0* libgnome-keyring0 python* python-minimal*
pkg_force_2_be_rm="python2.4-minimal python2.4 gksu gksudo gnome-keyring gnome-keyring-manager libgnome-keyring0 libgksu2-0 libgnome-keyring0"

# ///Note/// This should be the last one after any apt-get.
# clean unnecessary backup file to save space
clean_unnecessary_backup_file_in_boot
# prevent "W: Couldn't stat source package list http://..." error message
perl -pi -e "s/^/#/" /etc/apt/sources.list
apt-get update

# Remove grpck option "-p". This is only a workaround since the issue has not been fixed in Debian
# Ref: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=638263
remove_grpck_opt_p

if dpkg -L deborphan &>/dev/null; then	   
   echo "Removing deborphan..."
   apt-get --yes --force-yes --purge remove deborphan
fi

packages=$(LANG=C dpkg --list | grep -E "^rc" | awk '{print $2}')
[ -n "$packages" ] && dpkg --purge $packages

for i in $pkg_force_2_be_rm; do
  dpkg -L $i 2>/dev/null | xargs -I % bash -c "test -f % && rm -f %"
done

# Enable ntfs module
cat <<-LB_END >> /etc/live/boot.conf
# Added by GParted live
DISABLE_NTFS=false
LB_END

# We have to run update-initramfs so that the new busybox/cdromid/... will be in initramfs. This should be after dirty_hacking_rm_files_for_ocs_live since some files which we do not want (e.g. plymouth) might be included in initrd.
[ "$update_initramfs_flag" = "yes" ] && update-initramfs -u

# Create a list of all packages:
dpkg --get-selections > /root/gparted-livecd-packages.txt

# Restore gparted locales 
echo "Restoring gparted locales..."
( cd /usr/share/locale
  tar -xf /gparted-locale.tar
  rm -f /gparted-locale.tar
)
# After this, even if live helper to use apt to install squashfs and memtest, although the gparted locales will be removed, but they are not in the directory that will be packed by mksquashfs. Therefore, we can keep those gparted locales in filesystem.squashfs.

### THE END ###
# DO NOT PUT ANY SCRIPT AFTHER THIS!!!
# kill this program before creating squashfs filesystem.
rm -rf /live-hook-dir
